﻿using System;
using Microsoft.Phone.Scheduler;

namespace Stopwatch.TimerScheduledTask
{
    public sealed class TaskHelper
    {
        public const string TASK_NAME = "Stopwatch-Timer-ScheduledTask";

        public static void New()
        {
            try
            {
                if (ScheduledActionService.Find(TASK_NAME) != null)
                {
                    return;
                }

                PeriodicTask task = new PeriodicTask(TASK_NAME);
                task.Description = "Stopwatch Timer ScheduledTask.";
                ScheduledActionService.Add(task);
            }
            catch { }
        }

        public static void Launch()
        {
            try
            {
                ScheduledActionService.LaunchForTest(TASK_NAME, TimeSpan.FromSeconds(1));
            }
            catch { }
        }

        public static void Remove()
        {
            try
            {
                if (ScheduledActionService.Find(TASK_NAME) == null) { return; }
                ScheduledActionService.Remove(TASK_NAME);
            }
            catch { }
        }
    }
}
